﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;

namespace Guwave.MonitorDevice.Utility
{
    public class HttpHelper
    {
        /// <summary>
        /// POST 请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">数据</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="acceptType">接收类型</param>
        /// <returns></returns>
        public static string SendDataByPost(string url, string postData, string contentType,
                                            string acceptType, bool sslCertificate = false)
        {
            try
            {
                if (sslCertificate)
                {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                }
                //https请求需要
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = contentType;
                request.Accept = acceptType;
                request.Method = "POST";
                Stream requestStream = request.GetRequestStream();
                byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                requestStream.Close();
                WebResponse respone = request.GetResponse();
                Stream responeStream = respone.GetResponseStream();
                StreamReader reader = new StreamReader(responeStream, Encoding.UTF8);
                string result = reader.ReadToEnd();

                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// PUT 请求
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">数据</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="acceptType">接收类型</param>
        /// <returns></returns>
        public static string SendDataByPut(string url, string postData, string contentType,
                                            string acceptType, bool sslCertificate = false)
        {
            try
            {
                if (sslCertificate)
                {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                }
                //https请求需要
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = contentType;
                request.Accept = acceptType;
                request.Method = "PUT";
                Stream requestStream = request.GetRequestStream();
                byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
                requestStream.Write(postDataBytes, 0, postDataBytes.Length);
                requestStream.Close();
                WebResponse respone = request.GetResponse();
                Stream responeStream = respone.GetResponseStream();
                StreamReader reader = new StreamReader(responeStream, Encoding.UTF8);
                string result = reader.ReadToEnd();

                return result;
            }
            catch (Exception ex)
            {
             
                return null;
            }
        }
    }
}
